package concordia.comp6231.server;
// A server for the Hello object

import java.util.Arrays;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;

import concordia.comp6231.boxOfficeService.MyService;
import concordia.comp6231.boxOfficeService.MyServiceHelper;


public class BoxOfficeServer {

	public static void main(String args[]) {
		try {
			final String boxOfficeID = args[0];
			final String showsBaseDirectory = args[1];

			String[] orbArgs = Arrays.copyOfRange(args, 2, args.length);
			// create and initialize the ORB
			ORB orb = ORB.init(orbArgs, null);

			// get reference to rootpoa & activate the POAManager
			POA rootpoa = (POA) orb.resolve_initial_references("RootPOA");
			rootpoa.the_POAManager().activate();

			// create servant and register it with the ORB
			BoxOfficeImpl boxOfficeImpl = new BoxOfficeImpl();
			boxOfficeImpl.setORB(orb);
			boxOfficeImpl.setBoxOfficeID(boxOfficeID);
			boxOfficeImpl.setShowsBaseDirectory(showsBaseDirectory);

			// get object reference from the servant
			org.omg.CORBA.Object ref = rootpoa.servant_to_reference(boxOfficeImpl);
			// and cast the reference to a CORBA reference
			MyService href = MyServiceHelper.narrow(ref);

			// get the root naming context
			// NameService invokes the transient name service
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			// Use NamingContextExt, which is part of the
			// Interoperable Naming Service (INS) specification.
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			// bind the Object Reference in Naming
			String objName = boxOfficeID;
			NameComponent path[] = ncRef.to_name(objName);
			ncRef.rebind(path, href);
			System.out.println("BoxOffice Service ready and waiting ...");
			// wait for invocations from clients
			orb.run();
		}

		catch (Exception e) {
			System.err.println("ERROR: " + e);
			e.printStackTrace(System.out);
		}

		System.out.println("HelloServer Exiting ...");

	} // end main
} // end class
